<template>
  <z-publicFrame>
    <template #header>
      <el-page-header @back="goBack" content="修改">
      </el-page-header>
    </template>
    <template #container>
      <div class="formStyle">
        <el-form ref="form" :model="form" :rules="rules" label-width="100px" label-position="left">
          <ZForm v-for="(item, index) in formView" :key="index" :form="item" v-model="form[item.model]"></ZForm>
          <el-form-item>
            <el-button type="primary" @click="handleUpdate">立即修改</el-button>
          </el-form-item>
        </el-form>
      </div>
    </template>
  </z-publicFrame>
</template>

<script>
import PublicFrame from '../../components/CrudTempalte/PublicFrame'
const formView = [
  {
    type: 'text',
    model: 'name',
    label: '服务器名称',
    rule: {
      message: '请输入服务器名称'
    }
  },
  {
    type: 'text',
    model: 'flag',
    label: '服务器标志',
    rule: {
      message: '请输入服务器标志'
    }
  },
  {
    type: 'radio',
    model: 'status',
    label: '状态',
    radio: [
      { label: '正常', value: '正常' },
      { label: '异常', value: '异常' }
    ],
    rule: {
      message: '请选择状态'
    }
  },
  {
    type: 'text',
    model: 'serverOption',
    label: '服务器配置'
  }
]

function init() {
  const form = { code: '' }
  const rules = { }
  for (let i = 0; i < formView.length; i++) {
    let formViewElement = formView[i]
    let initValue = formViewElement.initValue
    let rule = formViewElement.rule
    let model = formViewElement.model
    if (initValue) {
      initValue = formViewElement.initValue
    } else {
      initValue = ''
    }
    form[model] = initValue
    //  rule
    if (rule) {
      rules[model] = [
        {
          required: true,
          message: rule.message,
          trigger: 'blur'
        }
      ]
    }
  }
  return { form, rules }
}
const { form, rules } = init()
export default {
  name: 'add',
  components: {
    'z-publicFrame': PublicFrame
  },
  data() {
    return {
      api: {
        update: this.$api.fileServer.update,
        detail: this.$api.fileServer.detail
      },
      formView,
      form,
      rules
    }
  },
  methods: {
    goBack() {
      this.$router.go(-1)
    },
    handleUpdate() {
      this.$httpTemplate({ url: this.api.update, paras: this.form }, e => {
        this.goBack()
      })
    },
    handleDetail(code) {
      this.$httpTemplate({ url: this.api.detail, paras: { code: code } }, data => {
        for (let item in data.data.data) {
          if (data.data.data[item].code) {
            this.$beanUtils.copyProperties(data.data.data[item], this.form)
          }
        }
      })
    }
  },
  created() {
    const code = this.$route.query.code
    this.handleDetail(code)
  }
}
</script>

<style scoped>
  .el-page-header {
    margin: 15px 0;
    overflow: hidden;
  }
  div.formStyle {
    margin-top: 30px;
    width: 600px;

  }

</style>
